package io.github.luyanfei



import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
class QuestionController {

	static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
	
	def beforeInterceptor = {
		if(!QuestionType.values()*.name.find {it == params['qtype']}) {
			response.sendError(403)
			return false
		}
	}

	def index(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		respond Question.findAllByQtype(params.qtype), model:[questionInstanceCount: Question.countByQtype(params.qtype)]
	}

	def show(Question questionInstance) {
		respond questionInstance
	}

	def create() {
		respond new Question(params)
	}

	@Transactional
	def save(Question questionInstance) {
		if (questionInstance == null) {
			notFound()
			return
		}

		if (questionInstance.hasErrors()) {
			respond questionInstance.errors, view:'create'
			return
		}

		questionInstance.save flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.created.message', args: [
					message(code: 'question.label', default: 'Question'),
					questionInstance.id
				])
				redirect questionInstance
			}
			'*' { respond questionInstance, [status: CREATED] }
		}
	}

	def edit(Question questionInstance) {
		respond questionInstance
	}

	@Transactional
	def update(Question questionInstance) {
		if (questionInstance == null) {
			notFound()
			return
		}

		if (questionInstance.hasErrors()) {
			respond questionInstance.errors, view:'edit'
			return
		}

		questionInstance.save flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.updated.message', args: [
					message(code: 'Question.label', default: 'Question'),
					questionInstance.id
				])
				redirect questionInstance
			}
			'*'{ respond questionInstance, [status: OK] }
		}
	}

	@Transactional
	def delete(Question questionInstance) {

		if (questionInstance == null) {
			notFound()
			return
		}

		questionInstance.delete flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.deleted.message', args: [
					message(code: 'Question.label', default: 'Question'),
					questionInstance.id
				])
				redirect action:"index", method:"GET"
			}
			'*'{ render status: NO_CONTENT }
		}
	}

	protected void notFound() {
		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.not.found.message', args: [
					message(code: 'question.label', default: 'Question'),
					params.id
				])
				redirect action: "index", method: "GET"
			}
			'*'{ render status: NOT_FOUND }
		}
	}
}
